package com.adulcimer.controllers.admin;

import com.adulcimer.controllers.BaseController;
import com.adulcimer.core.common.CheckRole;
import com.adulcimer.core.tools.DemonPagingModel;
import com.adulcimer.core.tools.R;
import com.adulcimer.models.dto.DemonIdsStringDto;
import com.adulcimer.models.dto.user.UserAddDto;
import com.adulcimer.models.dto.user.UserEnableDto;
import com.adulcimer.models.dto.user.UserUpdateDto;
import com.adulcimer.models.vo.UserVo;
import com.adulcimer.services.AccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Simon
 * @create 2025/7/29
 * @Description
 **/
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理后台接口", description = "用户管理后台接口")
public class UserController extends BaseController {

    @Resource
    private AccountService accountService;

    @PostMapping("/enable")
    @Operation(summary = "更改启用或禁用状态")
    @CheckRole("admin")
    public R<String> enable(@RequestBody @Validated UserEnableDto userEnableDto){
        accountService.enable(userEnableDto);
        return R.ok();
    }

    @GetMapping("/list")
    @CheckRole("admin")
    public R<DemonPagingModel<List<UserVo>>> list(@RequestParam(defaultValue = "1") Integer pageNumber,
                                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                                  @RequestParam(required = false) String keyword){
        DemonPagingModel<List<UserVo>> listDemonPagingModel = accountService.list(pageNumber, pageSize, keyword);
        return R.ok(listDemonPagingModel);
    }

    @PostMapping("/update")
    @CheckRole("admin")
    public R<String> update(@RequestBody @Validated UserUpdateDto userUpdateDto){
        accountService.update(userUpdateDto);
        return R.ok();
    }

    @PostMapping("/delete")
    @CheckRole("admin")
    public R<String> delete(@RequestBody DemonIdsStringDto ids){
        accountService.delete(ids.getIds());
        return R.ok();
    }

    @PostMapping("/add")
    @CheckRole("admin")
    public R<String> add(@RequestBody @Validated UserAddDto dto){
        accountService.add(dto);
        return R.ok();
    }
}
